package fr.umlv.android.caveAVin.activities;

import java.util.ArrayList;
import java.util.Iterator;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import fr.umlv.android.caveAVin.beans.WineCellar;
import fr.umlv.android.caveAvin.sqlite.WineCellarBDD;



public class WineCellarList extends ListActivity {
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
//		TextView text = new TextView(this);
//		text.setText("NTA HMAR");
		
//		setContentView(R.layout.wine_cellar_list2);
//		
//		ajoutBouton = (Button)findViewById(R.id.AjoutCave);
//		
//		ajoutBouton.setOnClickListener(new View.OnClickListener() {
//			public void onClick(View v) {
//				Log.e("CLIQUE ", "c'est le bouton ajout Cave");
//			}
//		});	
		
		// recuperer id du currentUser
		Intent myLocalIntent = getIntent();
		Bundle myLocalBundle = myLocalIntent.getExtras();
		int currentIdUser = myLocalBundle.getInt("idCurrentUser");
		// recup la liste des cave a vin de ce user
		ArrayList<WineCellar> list = WineCellarBDD.getAllWineCellarForCurrentUser(currentIdUser);
		
		String[] tabs = new String[list.size()];
		Iterator<WineCellar> it = list.iterator();
		int i = 0;
		while (it.hasNext() && i < list.size()) {
			WineCellar wineCellar = it.next();
			tabs[i] = wineCellar.getName();
			i++;
		}
		Log.e("FIRST ", "par la");
//		listView = (ListView)findViewById(R.id.ListView01);
//		if(ajoutBouton != null)	listView.addHeaderView(ajoutBouton);
		
		
		setListAdapter(new ArrayAdapter<String>(this,R.layout.wine_cellar_list, tabs));
		ListView listView = getListView();
//		if(ajoutBouton != null)	listView.addFooterView(ajoutBouton);
//		listView.addFooterView(text);
		listView.setTextFilterEnabled(true);
		listView.clearChoices();
		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		listView.setDividerHeight(1);
		Log.e("SECOND ", "par la");
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// When clicked, show a toast with the TextView text
				Toast.makeText(getApplicationContext(),((TextView) view).getText(), Toast.LENGTH_SHORT).show();
			}
		});
		
	}
}
